package com.audaque.springboot.foshanupload.web.webdemo.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.audaque.springboot.foshanupload.core.util.QRCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-08 02:07:07
 */
@RestController
@RequestMapping("qrcode")
public class QrCodeController  {

    private static final String RootPath="E:\\QRCode";
    private static final String FileFormat=".png";

    private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    //生成二维码并将其存放于本地目录
    @GetMapping("generate2Path")
    public void generateV1(String content){
        String fileName=LOCALDATEFORMAT.get().format(new Date());
        File file = new File(RootPath);
        QRCodeUtil.createCodeToFile(content,file,fileName+FileFormat);

    }

    //生成二维码并将其返回给前端调用者
    @GetMapping("generate2Stream")
    public void generateV2(String content, HttpServletResponse servletResponse) throws IOException {
        QRCodeUtil.createCodeToOutputStream(content,servletResponse.getOutputStream());
    }


    @Resource
    QrConfig qrconig;

    @GetMapping("generate2Path2")
    public void generate2Path2(String content ) {
        String fileName=LOCALDATEFORMAT.get().format(new Date());
        File file = new File(RootPath);
        QrCodeUtil.generate(content, qrconig, file);
    }

    //输出到流
    @GetMapping("generate2Stream2")
    public void generate2Stream2(String content, HttpServletResponse response) throws IOException {
        QrCodeUtil.generate(content, qrconig, "png", response.getOutputStream());
    }
}
